function userMessageS(){
    return new Promise((reslove,reject)=>{
        axios({
            url:'/picture/session',
        }).then((data)=>{
            reslove(data)
        }).catch((err)=>{
            reject(err)
        })
    })
}
let sessionUserid = null
let postId = document.location.href.split('=')[1]
//确认评论
let textArea = document.getElementsByTagName('textarea')[0]
userMessageS().then(data=>{
    console.log(data);
    if(data.data.session){
        sessionUserid=data.data.session.id
    }
    console.log(sessionUserid);


    // userId = 6


// 封装选择器函数
function $(selector){
    var c=selector.substring(0,1);//获取第一个字符
    if(c=="#"){
        return document.getElementById(selector.substring(1,selector.length));//返回相应的元素
    }else if(c == '.'){
        return document.getElementsByClassName(selector.substring(1,selector.length))
    }else{
        return document.getElementsByTagName(selector);
    }
}

//弹窗的样式
function alerter(str){
	$('.alert')[0].style.opacity = '1'
	$('.alert')[0].style.top = '40%'
	$('.alert')[0].children[1].innerHTML = str
	setTimeout(function(){
		$('.alert')[0].style.opacity = '0'
		$('.alert')[0].style.top = '30%'
	},1000)
}


//判断时间差的方法
function timeDiffer(pre,now){
    let preTime = pre.split('-')
        preYear = preTime[0]//发布帖子的年份
        preMonth = preTime[1]//发布帖子的月份
        preMore = preTime[2].split(':')//发布帖子的日 时分秒
        preDayHour = preMore[0].split(' ')//发布帖子的日 时
        preDay = preDayHour[0]//发布帖子的日
        preHour = preDayHour[1]//发布帖子的时
        premin = preMore[1]//发布帖子的分
        preSecond = preMore[2]//发布帖子的秒


        nowYear = now.getFullYear()//当前日期的年
        nowMonth = now.getMonth()//月
        nowDay = now.getDate()//日
        nowHour = now.getHours()
        nowMin = now.getMinutes()
        nowSecond = now.getSeconds()

        day = null

    if(preDay == nowDay){
        day = '今天'
    }else if((preDay==31 && nowDay==1)||(preDay==30 && nowDay==1)||(preDay==29 && nowDay==1)||(preDay==28 && nowDay==1)){
        day = '昨天'
    }else if(nowDay - preDay == 1){
        day = '昨天'
    }else{
        day = `${preMonth}月${preDay}日`
    }
    return `${day} ${preHour}:${premin}`
}



//点头像跳转到个人主页
$('.followHeadImg')[0].onclick = function(){
    window.location.href = `/picture/picture?headId=${this.getAttribute('data-userid')}`
}


//放上去时间
let time = new Date()
$('.followTime')[0].innerHTML = timeDiffer($('.followTime')[0].innerHTML,time)





//第二个分类名字放进去
axios.get(`/index/getFatherName?id=${$('.classTip')[0].getAttribute('data-classifyId')}`)
.then(data=>{
    $('.classTip')[0].innerHTML = `#${data.data.data.data.classfyName}`
})
.catch(err=>{
    console.log(err)
})


//喜欢得状态
if(sessionUserid){
    axios.get(`/index/likeStatus?postId=${postId}&userId=${sessionUserid}`)
    .then(data=>{
        let loveColor = null
        if(data.data.data.data == 0){//0是没有点赞
            loveColor = '#777777'
        }else{
            loveColor = '#D6222E'
        }
        $('.followLike')[0].style.color = loveColor
    })
    .catch(err=>{
        console.log(err)
    })

}else{
    $('.followLike')[0].style.color = '#777777'
}
//收藏得状态

if(sessionUserid){
    axios.get(`/index/lovePost?postId=${postId}&userId=${sessionUserid}`)
    .then(data=>{
        let loveColor = null
        if(data.data.data.data == 0){//0是没有点赞
            loveColor = '#777777'
        }else{
            loveColor = '#D6222E'
        }
        $('.followLove')[0].style.color = loveColor
    })
    .catch(err=>{
        console.log(err)
    })
}else{
    $('.followLove')[0].style.color = '#777777'
}


//选择举报原因
var reportList = $('.reportList')
    reportInput = $('.reportInput')[0]
    reportUl = $('.reportUl')[0]

reportInput.onclick = ()=>{
    if(reportUl.style.display === 'block'){
        reportUl.style.display = 'none'

    }else{
        reportUl.style.display = 'block'
    }
}
for(let i = 0;i < reportList.length;i++){
    reportList[i].onclick = ()=>{
        reportInput.value = reportList[i].innerHTML
        reportUl.style.display = 'none'
    }
}
//关闭举报
var closeReport = $('.closeReport')[0]
closeReport.onclick = ()=>{
    reportBox.style.display = 'none'

}


//照片数组，改变布局
function picLayout(picArr){
    let aa = picArr.split('[')[1].split(']')[0]
    let arr = aa.split(',')
    let str = ''
    for(let i = 0;i < arr.length;i++){
        str += `
        <li><img onload=imgOnload(this) onclick=showMaxPic() style="cursor:url('../../public/img/larger.cur'),auto" src="${arr[i]}"/></li>
        `
    }
    return str
}


//小图片布局
function smallPicLayout(picArr){
    let aa = picArr.split('[')[1].split(']')[0]
    let arr = aa.split(',')
    let str = ''
    for(let i = 0;i < arr.length;i++){
        str += `
        <li><img src="${arr[i]}" class="smallPic"/></li>
        `
    }
    return str
}



let lookIndex = 0
//要把图片放上去
axios.get(`/index/getPost?postId=${postId}&userId=${sessionUserid}`)
    .then(data=>{
        let path = data.data.data.data.path
        // console.log(data)
		$('.followImg')[0].children[0].innerHTML = picLayout(path)
		$('.smallBox')[0].children[0].innerHTML = smallPicLayout(path)
        showIndex(lookIndex)
    })
    .catch(err=>{
        console.log(err)
    })



function showSmallPic(){
    $('.followImg')[0].style.display = 'block'
    $('.lookPic')[0].style.display = 'none'
}






//放到放大图片上之后左右翻效果
let isLeft = null
$('.maxImg')[0].onmousemove = function(ev){
    if($('.smallBox')[0].children[0].children.length == 1){
        //如果小图片只有一张
        isLeft = null
    }else{
        if(ev.clientX - this.offsetLeft >= this.offsetWidth/2){
            //如果鼠标在图片右边
            if(lookIndex == $('.smallBox')[0].children[0].children.length-1){
                //当前是最后一张
                isLeft = true
            }else{
                isLeft = false
            }
        }else{
            //如果鼠标在右边
            if(lookIndex == 0){
                //当前是第一张
                isLeft = false
            }else{
                isLeft = true
            }
        }
    }
    
    if(isLeft){
        $('.maxImg')[0].style.cursor = "url('../../public/img/lefttt.cur'),auto"
    }else if(isLeft == false){
        $('.maxImg')[0].style.cursor = "url('../../public/img/righttt.cur'),auto"
    }else{
        $('.maxImg')[0].style.cursor = "default"
    }
}
$('.maxImg')[0].onclick = function(){
    console.log(lookIndex)
    if(isLeft){
        if(lookIndex == 0){
            lookIndex = $('.smallBox')[0].children[0].children.length-1
        }else{
            lookIndex -= 1
        }
        showIndex(lookIndex)
    }else if(isLeft == false){
        //当前显示的是最后一张但是有bug
        if($('.smallBox')[0].children[0].children.length-1 == lookIndex){
            lookIndex = 0
        }else{
            lookIndex += 1
        }
        showIndex(lookIndex)
    }else{
        return false
    }
}
// //放的是图片下标
function showIndex(i){
    lookIndex = i
    for(let a = 0;a < $('.smallPic').length;a++){
        if(a == i){
            $('.smallPic')[a].style.padding = '1px'
            $('.smallPic')[a].style.border = 'solid 2px #D6222E'
        }else{
            $('.smallPic')[a].style.padding = '0px'
            $('.smallPic')[a].style.border = 'none'
        }
    }
    //判断图片宽高，判断不出来，图片宽高可能和框有点冲突
    $('.maxImg')[0].setAttribute('src',$('.smallPic')[i].getAttribute('src'))
}
// //收起大图片！！！！
$('.closeMax')[0].children[0].onclick = showSmallPic





//把帖子所属专辑信息放进去
let albumId = $('.fromAlbum')[0].getAttribute('data-albumId')

axios.get(`/index/getAlbum?albumId=${albumId}`)
    .then(data=>{
        console.log(data)
        let ans = data.data.data.data
        $('.albumSmallPic')[0].innerHTML = `
        <img src=${ans.cover} style="width:100%;height:100%;object-fit: cover;object-position: center center;cursor:pointer" style="cursor:pointer" onclick='toAlbumDetail()'/>
        `
        $('.toAlbum')[0].children[1].innerHTML = ans.name
        $('.loveAlbumNum')[0].innerHTML = `${ans.collect}人收藏`
    })
    .catch(err=>{
        console.log(err)
    })







//点评论之后要出来东西
let ifAdd = true
$('.followTranlate')[0].onclick = function(){
	if(ifAdd){
        if(sessionUserid){
            addHeight()
        }else{
            // location.href = '/login'
            toLoginTipFunc()
        }
	}else{
		cutHeight()
	}
}
// let height = 0
// let timer = null
// function addHeight(){
// 	height += 5
// 	$('.translateDiv')[0].style.height = `${height}px`
// 	if(height >= 180){
// 		clearTimeout(timer)
// 		ifAdd = false
// 	}else{
// 		timer = setTimeout(addHeight,1)
// 	}
// }


// let cutTimer = null
// function cutHeight(){
// 	height -= 5
// 	$('.translateDiv')[0].style.height = `${height}px`
// 	if(height <= 0){
// 		clearTimeout(cutTimer)
// 		ifAdd = true
// 	}else{
// 		cutTimer = setTimeout(cutHeight,1)
// 	}
// }





//取消评论
$('.deleteTrans')[0].onclick = ()=>{
	textArea.value = ''
	cutHeight()
}
})

// $('.closeMax')[0].children[0].onclick = showSmallPic
let height = 0
let timer = null
function addHeight(){
	height += 5
	$('.translateDiv')[0].style.height = `${height}px`
	if(height >= 180){
		clearTimeout(timer)
		ifAdd = false
	}else{
		timer = setTimeout(addHeight,1)
	}
}


let cutTimer = null
function cutHeight(){
	height -= 5
	$('.translateDiv')[0].style.height = `${height}px`
	if(height <= 0){
		clearTimeout(cutTimer)
		ifAdd = true
	}else{
		cutTimer = setTimeout(cutHeight,1)
	}
}
//正常的遍历评论
function comment(pageNum){
    // console.log(pageNum);
    return new Promise((reslove,reject)=>{
        axios({
            url:'/comment/searchCommentByForumId',
            headers: {
                'Content-Type':'application/x-www-form-urlencoded',
            },
            params:{
                forumId:postId,
                pageNow:pageNum,
                size:6
            }
        }).then(value=>{
            reslove(value)
        }).catch(err=>{
            reject(err)
        })
    })
}
let size=1
let tagVessels=document.querySelector('.commentVessels')
    function commentMessage(size){
        comment(size).then(data=>{
            $('.transBtn')[0].onclick = ()=>{
                let ba=size*6<data.data.key.data.total
                reply(ba)
            }
            if(data.data.key.data.list==0){
                tagVessels.innerHTML+=`
                    <div class="userEmpty">还没有评论哦去ヾ(≧▽≦*)</div>
                `
            }else{
                let message=data.data.key.data.list
                let contentHeadImg =document.querySelector('.contentHeadImg ').getAttribute('data-userid')
                    for(let i=0,len=message.length;i<len;i++){
                        let messagei=message[i]
                        // console.log(messagei.sonComment.list[0]);
                        messagei.content=htmlEscape(messagei.content)
                        if(messagei.sonComment.list.length!=0){
                            tagVessels.innerHTML+=`
                            <div class="comment" commentid="${messagei.id}" userid="${messagei.ownerId}">
                                <div class="whoComment">
                                    <img src="${messagei.ownerAvatar}"/>
                                    <div class="nameTime">
                                        <h4 class="c">${messagei.ownerName}</h4>
                                        <p>${messagei.time}</p>
                                    </div>
                                    <div class="operation">
                                        <span class="aaa">
                                            ${messagei.ownerId==sessionUserid||sessionUserid==contentHeadImg?'<span class="reportComment" onclick="delect(this)">删除</span>':''}
                                            <span class="commentComment" onclick="replayFather(this)">回复</span>
                                        </span>
                                    </div>
                                </div>
                                <div class="commentContent">
                                    ${messagei.content}
                                </div>
                                <div class="replayComment">

                                </div>
                                <div class="childeCommentVessles">
                                    <div class="chiledVessles" commentid="${messagei.sonComment.list[0].id}">
                                        <div class="childeHeard" style="background-image:url(${messagei.sonComment.list[0].ownerAvatar})"></div>
                                        <div class="childeContent">
                                            <div>
                                                <span style="color: #00a1d6;" userid='${messagei.sonComment.list[0].ownerId}' class="c">${messagei.sonComment.list[0].ownerName}</span>
                                                <span>回复</span>
                                                <span style="color: #00a1d6;" userid='${messagei.sonComment.list[0].repliedOwnerId}' class="c">@${messagei.sonComment.list[0].replyName}</span>
                                                <span>:${messagei.sonComment.list[0].content}</span>
                                            </div>
                                            <span class="aaa">
                                                <!--子评论删除-->
                                                <!--${messagei.ownerId==sessionUserid||messagei.ownerId==contentHeadImg?'<span class="reportComment" onclick="delectc(this)">删除</span>':''}
                                                <span class="commentComment" onclick="replyChildeCommentS(this)">回复</span>-->
                                            </span>
                                        </div>
                                    </div>
                                    <div class="useContent">
                                        <div class="leftS">共${messagei.sonComment.total}条回复</div>
                                        ${messagei.sonComment.total>=1?`<div class="rightS" style="color: #00a1d6;" onclick="show(this)">展开</div>`:''}
                                    </div>
                                </div>
                            </div>
                            `
                        }else{
                            tagVessels.innerHTML+=`
                            <div class="comment" commentid="${messagei.id}" userid="${messagei.ownerId}">
                                <div class="whoComment">
                                    <img src="${messagei.ownerAvatar}"/>
                                    <div class="nameTime">
                                        <h4 class="c">${messagei.ownerName}</h4>
                                        <p>${messagei.time}</p>
                                    </div>
                                    <div class="operation">
                                        <span class="aaa">
                                            ${messagei.ownerId==sessionUserid||sessionUserid==contentHeadImg?'<span class="reportComment" onclick="delect(this)">删除</span>':''}
                                            <span class="commentComment" onclick="replayFather(this)">回复</span>
                                        </span>
                                    </div>
                                </div>
                                <div class="commentContent">
                                    ${messagei.content}
                                </div>
                                <div class="replayComment">

                                </div>
                                <div class="childeCommentVessles">

                                </div>
                            </div>
                            `
                        }
                    }
                    let node=tagVessels.lastElementChild
                    let io=new IntersectionObserver(config=>{
                        if(config[0].intersectionRatio>0){
                            //停止观察
                            io.disconnect()
                            if(size*6<data.data.key.data.total){
                                size++
                                commentMessage(size)
                            }
                        }
                    })
                    io.observe(node)
            }
        }).catch(err=>{
            console.log(err);
        })
    }
    commentMessage(size)

//图片居中
function imgOnload(e) {
	var wid = e.parentNode.offsetWidth;//父容器限制高度
	var hei = e.parentNode.offsetHeight;//父容器限制宽度
	var realWidth = e.width; //储存图片实际宽度
	var realHeight = e.height; //储存图片实际高度
	//获取图片宽高比
	var realwh = e.width / e.height;
	if(realwh > wid/hei) {
		var w = ((hei / realHeight) * realWidth) + 'px';
		var h = hei + 'px';
		var l = '-' + ((hei / realHeight) * realWidth - wid) / 2 + 'px';
		e.style.width = w; //等比缩放宽度
		e.style.height = h; //跟div高度一致
		e.style.left = l; //设置图片相对自己位置偏移为img标签的宽度-高度的一半
		e.style.position = 'relative';
	} else {
		var w = wid +'px';
		var h = ((wid / realWidth) * realHeight) + 'px';
		var t = '-' + ((wid / realWidth) * realHeight - hei) / 2 + 'px';
		e.style.width = w; //等比缩放宽度
		e.style.height = h; //跟div高度一致
		e.style.top = t; //设置图片相对自己位置偏移为img标签的宽度-高度的一半
		e.style.position = 'relative';
	}
}



//点赞或者取消点赞

function likeFunc(a){
    if(sessionUserid==undefined){
        // location.href='/login'
        toLoginTipFunc()
    }else{
        axios({
            method:"post",
            url:'/index/like',
            data:{
                userId:sessionUserid,
                postId:postId
            }
            
        }).then((data)=>{
            let res = data.data.data.data
            let inner = Number(a.children[1].innerHTML)
            if(res == 1){//点赞
                inner += 1
                a.style.color = '#D6222E'
            }else{//取消
                a.style.color = '#777777'
                inner -= 1
            }
            a.children[1].innerHTML = inner
        }).catch((err)=>{
            console.log(err)
        })
    }
}
//收藏或者取消收藏
function loveFunc(a){
    if(sessionUserid==undefined){
        // location.href='/login'
        toLoginTipFunc()
    }else{
        axios({
            method:"post",
            url:'/index/love',
            data:{
                userId:sessionUserid,
                postId:postId
            }
            
        }).then((data)=>{
            let res = data.data.data.data
            let inner = Number(a.children[1].innerHTML)
            if(res == 1){//点赞
                inner += 1
                a.style.color = '#D6222E'
            }else{//取消
                a.style.color = '#777777'
                inner -= 1
            }
            a.children[1].innerHTML = inner
        }).catch((err)=>{
            console.log(err)
        })
    }
}

//点举报！！
var reportBox = $('.reportBox')[0]

let reportReason = null
    reportType = 0
    reportUser = sessionUserid
    reportUserName = '我的页面好丑'
    reportcontentId = null
    reportedUser = null
    reportedUserName = null

function reportFunc(a){
    if(sessionUserid){
        reportcontentId = Number(a.getAttribute('data-id'))
        reportedUser = Number(a.getAttribute('data-reportedId'))
        reportedUserName = a.getAttribute('data-reportedUserName')
        reportBox.style.display = 'block'
    }else{
        // location.href = '/login'
        toLoginTipFunc()
    }
}
//确认举报
$('.reportBtn')[0].onclick = function(){
    reportReason = $('.reportInput')[0].value
    if(reportReason.length != 0){
        $('.reportEmpty')[0].style.display = 'none'
        axios({
            method:'post',
            data:{
                reportReason:reportReason,
                reportType:reportType,
                reportUser:reportUser,
                reportUserName:reportUserName,
                reportcontentId:reportcontentId,
                reportedUser:reportedUser
            },
            url:"/index/report"
        }).then(data=>{
            //举报成功之后
            reportBox.style.display = 'none'
            reportInput.value = ''
            tigs('举报成功，我们将根据您的举报做出相应处理！',true)
            // console.log(data)
        }).catch(err=>{
            console.log(err)
        })
    }else{
        $('.reportEmpty')[0].style.display = 'inline-block'
    }

}
//获得帖子照片的数组
function getPicLength(path){
    if(path == null){
        return ' '
    }else{
        let imgSrc = path.split('::')
        return imgSrc

    }
}

//隐藏或者显示大图片
function showMaxPic(){
    $('.followImg')[0].style.display = 'none'
    $('.lookPic')[0].style.display = 'block'
}

//点专辑封面要去专辑详情页
function toAlbumDetail(){
    window.location.href=`/picture/personmainpage?albumId=${$('.fromAlbum')[0].getAttribute('data-albumId')}`
} 




//点标签要去分类页面
function toclassify(a){
    axios.get(`/index/getFather?subClassifyId=${a.getAttribute('data-classifyId')}`)
    .then(data=>{
        window.location.href = `/picture/classfy?sonId=${a.getAttribute('data-classifyId')}&fatherId=${data.data.data.data}`
    })
    .catch(err=>{
        console.log(err)
    })
}



//主评论删除
function delect(th){
    let node=th.parentNode.parentNode.parentNode.parentNode
    axios({
        url:'/comment/delCommentById',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
        },
        params:{
            commentId:node.getAttribute('commentid'),
            userId:sessionUserid,
            formId:postId
        }
    }).then(value=>{
        console.log(postId);
        console.log(value);
        let message=value.data.key.data
        console.log(message);
        if(message=="删除成功"){
            showBlack(message)
            node.remove()
        }else{
            showBlack(message)
        }
    }).catch(err=>{
        console.log(err);
    })
}

//子评论删除
function delectc(th){
    let node=th.parentNode.parentNode.parentNode
    let nodes=node.parentNode.parentNode.getAttribute('commentid')
    //获取现在的页数
    let nowPage=node.parentNode.parentNode.lastElementChild
    axios({
        url:'/comment/delCommentById',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
        },
        params:{
            commentId:node.getAttribute('commentid'),
            userId:sessionUserid,
            formId:postId
        }
    }).then(value=>{
        let message=value.data.key.data
        if(message=="删除成功"){
            showBlack(message)
            //如果当前子评论没有分页，直接移出该元素，如果有分页则重新加载
            let pageNum=1
            if(nowPage.lastElementChild.previousElementSibling){
                pageNum=nowPage.lastElementChild.previousElementSibling.innerHTML
            }
            let childenVessles=nowPage.previousElementSibling
            if(nowPage.className=='page'){
                x(nodes).then(data=>{
                    if(pageNum>Math.ceil(data/10)){
                        pageNum=Math.ceil(data/10)
                    }
                    //删除成功获取子评论的总数
                    let length=data
                    //判断是否需要分页
                    All(nodes,pageNum,childenVessles,length)
                })
            }else{
                console.log(node);
                node.remove()
            }
        }else{
           showBlack(message)
        }
    }).catch(err=>{
        console.log(err);
    })
}

//展开
function show(th){
    let node=th.parentNode.parentNode
    // 调用函数将全部信息展示在页面上
    showChildeComment(node)
}


function showChildeComment(th){
    //将th中的元素清空
    th.innerHTML=``
    //清空之后判断共有多少条信息如果大于等于10则不分页，否则分页
    eachChildeComment(th)
}
function eachChildeComment(th,pageNum=1,size=10){
    let commentid=th.parentNode.getAttribute('commentid')
    axios({
        url:'/comment/searchCommentOfRootComment',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
        },
        params:{
            id:commentid,
            pageNum:pageNum,
            size:size
        }
    }).then(value=>{
        let message=value.data.key.data.list
        //先把条数写死
        let length=value.data.key.data.total
        //这里判断条数是否超过10
        if(length<=10){
            console.log(th);
            ltTen(message,th,length)
        }else{
            gtTen(message,th,length,1)
        }
    }).catch(err=>{
        console.log(err);
    })
}

//如果子评论小于等于10条数据
function ltTen(message,th){
    console.log(message);
    th.innerHTML=``
    let contentHeadImg=document.querySelector('.contentHeadImg')
    for(let i=0,len=message.length;i<len;i++){
        let messagei=message[i]
        messagei.content=htmlEscape(messagei.content)
        th.innerHTML+=`
        <div class="chiledVessles" commentid="${messagei.id}">
            <div class="childeHeard" style="background-image:url(${messagei.ownerAvatar})"></div>
            <div class="childeContent">
                <div>
                    <span style="color: #00a1d6;" userid='${messagei.ownerId}' class="c">${messagei.ownerName}</span>
                    <span>回复</span>
                    <span style="color: #00a1d6;" userid='${messagei.repliedOwnerId}' class="c">@${messagei.replyName}</span>
                    <span>:${messagei.content}</span>
                </div>
                <span class="aaa">
                    <!--子评论删除-->
                    ${messagei.ownerId==sessionUserid||messagei.ownerId==contentHeadImg?'<span class="reportComment" onclick="delectc(this)">删除</span>':''}
                    <span class="commentComment" onclick="replyChildeCommentS(this)">回复</span>
                </span>
                <div class="replyChildeWriteVessel"></div>
            </div>
        </div>
        `
    }
}

//如果子评论大于10条数据
function gtTen(message,th,len,n){
    th.innerHTML=``
    // console.log(th.parentNode);
    // console.log(th);
    let contentHeadImg=document.querySelector('.contentHeadImg')
    for(let i=0,len=message.length;i<len;i++){
        let messagei=message[i]
        messagei.content=htmlEscape(messagei.content)
        th.innerHTML+=`
        <div class="chiledVessles" commentid="${messagei.id}">
            <div class="childeHeard" style="background-image:url(${messagei.ownerAvatar})"></div>
            <div class="childeContent">
                <div>
                    <span style="color: #00a1d6;" userid='${messagei.ownerId}' class="c">${messagei.ownerName}</span>
                    <span>回复</span>
                    <span style="color: #00a1d6;" userid='${messagei.repliedOwnerId}' class="c">@${messagei.replyName}</span>
                    <span>:${messagei.content}</span>
                </div>
                <span class="aaa">
                    <!--子评论删除-->
                    ${messagei.ownerId==sessionUserid||messagei.ownerId==contentHeadImg?'<span class="reportComment" onclick="delectc(this)">删除</span>':''}
                    <span class="commentComment" onclick="replyChildeCommentS(this)">回复</span>
                </span>
                <div class="replyChildeWriteVessel"></div>
            </div>
        </div>
        `
    }
    th.parentNode.innerHTML+=`
        <div class="page">
            <!--这里点击加减号切换页数-->
            <span>共${len}条数据&ensp;</span>
            <span class="delx" onclick="delx(this)">-</span>
            <span class="numx">${n}</span>
            <span class="addx" onclick="addx(this)">+</span>
        </div>
    `
}


//主评论发布
function reply(ba){
    //确认评论
    // let textArea = document.getElementsByTagName('textarea')[0]
    let tagVessels=document.querySelector('.commentVessels')
    if(textArea.value==''){
        showBlack('请先输入内容')
    }else{
        axios({
            url:'/comment/insertComment',
            headers: {
                'Content-Type':'application/x-www-form-urlencoded',
            },
            params:{
                content:textArea.value,
                ownerId:sessionUserid,
                forumId:postId
            }
        }).then(value=>{
            let message=value.data.key.message
            console.log(message);
            if(message=="成功"){
                showBlack('评论成功')  
                textArea.value = ''
	            cutHeight()
                tagVessels.innerHTML=``
                size=1
                commentMessage(size)
            }else{
                alert(message)
            }
        }).catch(err=>{
            console.log(err);
        })
    }
}

//提醒窗口
function showBlack(h){
    let black=document.querySelector('.black')
    black.innerHTML=h
    black.style.display='block'
    setTimeout(()=>{
        black.style.opacity=1
        setTimeout(()=>{
            black.style.opacity=0
            setTimeout(()=>{
                black.style.display='none'
            },1000)
        },1000)
    },100)
}


//分页总数返回
function x(commentid){
    return new Promise((reslove,reject)=>{
        axios({
            url:'/comment/searchCommentOfRootComment',
            headers: {
                'Content-Type':'application/x-www-form-urlencoded',
            },
            params:{
                id:commentid,
                pageNum:1,
                size:10
            }
        }).then(value=>{
            let length=value.data.key.data.total
            reslove(length)
        }).catch(err=>{
            console.log(err);
        })
    })
}

//评论分页切换下一页
function addx(th){
    let node=th.parentNode.parentNode.getAttribute('commentid')
    let nodes=th.parentNode.previousElementSibling
    let nodeTop=th.previousElementSibling
    x(node).then(data=>{
        //判断页数是否大于最大页数，如果大于不最大页数则增加一页
        if(!(nodeTop.innerText>=Math.ceil(data/10))){
            nodeTop.innerHTML=parseInt(nodeTop.innerHTML)+1
            axios({
                url:'/comment/searchCommentOfRootComment',
                headers: {
                    'Content-Type':'application/x-www-form-urlencoded',
                },
                params:{
                    id:node,
                    pageNum:nodeTop.innerText,
                    size:10
                }
            }).then(value=>{
                let message=value.data.key.data.list
                console.log(message);
                let num=nodeTop.innerText
                let deleNode=th.parentNode.parentNode
                let deled=th.parentNode
                //将分页移除
                deleNode.removeChild(deled)
                gtTen(message,nodes,data,num)
            }).catch(err=>{
                console.log(err);
            })
        }
    }).catch(err=>{
        console.log(err);
    })
}

//评论分页切换上一页
function delx(th){
    let node=th.parentNode.parentNode.getAttribute('commentid')
    let nodes=th.parentNode.previousElementSibling
    let nodeTop=th.nextElementSibling
    x(node).then(data=>{
        // console.log(data);
        //判断页数是否大于最大页数，如果大于不最大页数则增加一页
        if(!(nodeTop.innerText<=1)){
            nodeTop.innerHTML=parseInt(nodeTop.innerHTML)-1
            axios({
                url:'/comment/searchCommentOfRootComment',
                headers: {
                    'Content-Type':'application/x-www-form-urlencoded',
                },
                params:{
                    id:node,
                    pageNum:nodeTop.innerText,
                    size:10
                }
            }).then(value=>{
                let message=value.data.key.data.list
                console.log(message);
                let num=nodeTop.innerText
                let deleNode=th.parentNode.parentNode
                let deled=th.parentNode
                //将分页移除
                deleNode.removeChild(deled)
                gtTen(message,nodes,data,num)
                th.parentNode.remove()
            }).catch(err=>{
                console.log(err);
            })
        }
    }).catch(err=>{
        console.log(err);
    })
}

//node：父评论的id,pageNum:第几页,message:数组,nodes:子评论的盒子,len:显示的条数,n:显示第几页
function All(node,pageNum,nodes,data){
    axios({
        url:'/comment/searchCommentOfRootComment',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
        },
        params:{
            id:node,
            pageNum:pageNum,
            size:10
        }
    }).then(value=>{
        let message=value.data.key.data
        console.log(message);
        console.log(message.total);
        if(message.total<=10){
            ltTen(message.list,nodes)
        }else{
            let re=nodes.nextElementSibling
            if(re){
                re.parentNode.removeChild(re)
            }
            gtTen(message.list,nodes,data,pageNum)
        }
    }).catch(err=>{
        console.log(err);
    })
}


//回复父评论
let replays=true
function replayFather(th){
    if(sessionUserid){
        if(replays){
            let node=th.parentNode.parentNode.parentNode.nextElementSibling.nextElementSibling
            console.log(node);
            node.innerHTML+=`
            <div class="transTop">
                <p>回复</p>
                <textarea placeholder="说点啥！！！！！" ></textarea>
            </div>
            <div class="transManyBtn">
                <p class="deleteTrans" onclick="delPlay(this)">取消</p>
                <p class="transBtn" onclick="replayChilde(this)">发送</p>
            </div>
            `
            node.style.height="180px"
            replays=false
        }
    }else{
        // window.location.href="/login"
        toLoginTipFunc()
    }
}

//取消评论父评论
function delPlay(th){
    let node=th.parentNode.parentNode
    console.log(node);
    node.style.height="0"
    setTimeout(()=>{
        node.innerHTML=``
    },300)
    replays=true
}

//确定评论父评论
function replayChilde(th){
    let content= th.parentNode.previousElementSibling.lastElementChild.value
    let repliedOwnerId=th.parentNode.parentNode.parentNode.getAttribute('userid')
    let replyId=th.parentNode.parentNode.parentNode.getAttribute('commentid')
    if(content==''){
        showBlack('请输入内容')
    }else{
        axios({
            url:'/comment/childeComment',
            headers: {
                'Content-Type':'application/x-www-form-urlencoded',
            },
            params:{
                content:content,
                ownerId:sessionUserid,
                forumId:postId,
                replyId:replyId,
                repliedOwnerId:repliedOwnerId
            }
        }).then(value=>{
            let message=value.data.key.message
            console.log(message);
            if(message=="成功"){
                showBlack('评论成功')
                let nodes=th.parentNode.parentNode.nextElementSibling
                let pageNum=1
                let node=th.parentNode.parentNode.parentNode.getAttribute('commentid')
                x(node).then(data=>{
                    All(node,pageNum,nodes,data)
                })
                let nodesss=th.parentNode.parentNode
                nodesss.style.height="0"
                setTimeout(()=>{
                    nodesss.innerHTML=``
                },300)
                replays=true
            }
        }).catch(err=>{
            console.log(err);
        })   
    }
}

function replyChildeCommentS(th){
    if(sessionUserid){
        let node=th.parentNode.parentNode.lastElementChild
        console.log(node);
        node.innerHTML+=`
        <div class="transTop">
            <p>回复</p>
            <textarea placeholder="说点啥！！！！！" ></textarea>
        </div>
        <div class="transManyBtn">
            <p class="deleteTrans" onclick="delPlay(this)">取消</p>
            <p class="transBtn" onclick="replayChildeChilde(this)">发送</p>
        </div>
        `
        node.style.height="180px"
        replays=false
    }else{
        // window.location.href='/login'
        toLoginTipFunc()
    }
}

function replayChildeChilde(th){
    console.log(th);
    //回复的内容
    let content=th.parentNode.parentNode.lastElementChild.previousElementSibling.lastElementChild.value
    let replyId=th.parentNode.parentNode.parentNode.parentNode.getAttribute('commentid')
    let rootId=th.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute('commentid')
    console.log(rootId);
    if(content==""){
        showBlack('请输入内容')
    }else{
        axios({
            url:'/comment/childeChilde',
            headers: {
                'Content-Type':'application/x-www-form-urlencoded',
            },
            params:{
                content:content,
                forumId:postId,
                replyId:replyId,
                rootId:rootId
            }
        }).then(value=>{
            let messag=value.data.key.message
            if(messag=="成功"){
                showBlack(messag)
                let nodes=th.parentNode.parentNode.parentNode.parentNode.parentNode
                console.log(nodes);
                let pageNum=1
                x(rootId).then(data=>{
                    console.log(data);
                    All(rootId,pageNum,nodes,data)
                })
                replays=true
            }else{
                showBlack(messag)
            }
        }).catch(err=>{
            console.log(err);
        })
    }
}

function htmlEscape(text){
    // console.log(text);
    return text.replace(/[<>&]/g,function(match,pos,originalText){
    switch(match){
    case "<":return "&lt";
    case ">":return "&gt";
    }
});
}
